<?php


namespace App\Http\Controllers\Admin\User;


use App\Http\Controllers\Admin\BaseController;
use App\Http\Requests\Admin\User\Permission\CreateRequest;
use App\Http\Requests\Admin\User\Permission\UpdateRequest;
use App\Models\Permission;
use App\Repositories\Admin\User\PermissionRepository;
use Illuminate\Http\Request;

class PermissionController extends BaseController
{
    public function __construct()
    {
        $this->repo = new PermissionRepository();
    }
    public function fetchInfo(Permission $permission){
        return $this->success($permission);
    }
    public function tree(Request $request){
        $type = $request->get('type');
        if($type==2){
            $where['pid'] = 0;
        }
        $tree = $this->repo->tree();
        return $this->success(compact('tree'));
    }
    public function create(CreateRequest $request){
        $data = $request->only('name','display_name','description','pid');
        $permission = $this->repo->create($data);
        return $permission?$this->success($permission,'添加成功'):$this->error('添加失败');
    }
    public function update(UpdateRequest $request,Permission $permission){
        $data = $request->only('name','display_name','description','pid');
        $permission = $this->repo->update($permission,$data);
        return !$permission ? $this->error('更新失败') : $this->success($permission, '更新成功');
    }

    public function delete(Permission $permission){
        return $this->repo->delete($permission)?$this->success([],'删除成功'):$this->error('删除失败');
    }
    public function fetchList(){}
}
